package cn.exrick.manager.controller;


import cn.exrick.common.pojo.DataTablesResult;
import cn.exrick.common.pojo.Result;
import cn.exrick.common.utils.ResultUtil;
import cn.exrick.manager.pojo.TbItem;
import cn.exrick.manager.pojo.TbNav;
import cn.exrick.manager.service.ContentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@Api(description = "导航栏列表信息")
public class NavController {

    private final static Logger log= LoggerFactory.getLogger(NavController.class);

    @Autowired
    private ContentService contentService;


    @RequestMapping(value = "/nav/{navId}",method = RequestMethod.GET)
    @ApiOperation(value = "通过ID获取导航栏信息")
    public Result<TbNav> getNavById(@PathVariable Long id){

        TbNav tbNav = contentService.getNavById(id);
        return new ResultUtil<TbNav>().setData(tbNav);
    }

    @RequestMapping(value = "/nav/list",method = RequestMethod.GET)
    @ApiOperation(value = "分页搜索排序获取导航栏列表")
    public DataTablesResult getNavList(int draw, int start, int length,String isParent,String type, @RequestParam("search[value]") String search,
                                        @RequestParam("order[0][column]") int orderCol, @RequestParam("order[0][dir]") String orderDir){


        //获取客户端需要排序的列
        String[] cols = {"checkbox", "a.name", "a.englishName", "a.is_parent","a.parent_id", "a.sort_order"};
        String orderColumn = cols[orderCol];
        if(orderColumn == null) {
            orderColumn = "a.sort_order";
        }
        //获取排序方式 默认为desc(asc)
        if(orderDir == null) {
            orderDir = "asc";
        }
        DataTablesResult result=contentService.getNavList(draw,start,length,isParent,type,search,orderColumn,orderDir);
        return result;
    }



    @RequestMapping(value = "/nav/del/{ids}",method = RequestMethod.DELETE)
    @ApiOperation(value = "删除导航栏")
    public Result<TbItem> deleteNav(@PathVariable Long[] ids){

        contentService.deleteNav(Arrays.asList(ids));
        return new ResultUtil<TbItem>().setData(null);
    }

    @RequestMapping(value = "/nav/add",method = RequestMethod.POST)
    @ApiOperation(value = "添加导航栏")
    public Result<TbNav> addNav(TbNav tbNav){
        contentService.addNav(tbNav);
        return new ResultUtil<TbNav>().setData(null);
    }

    @RequestMapping(value = "/nav/update",method = RequestMethod.POST)
    @ApiOperation(value = "编辑导航栏")
    public Result<TbNav> updateNav(TbNav tbNav){
        contentService.updateNav(tbNav);
        return new ResultUtil<TbNav>().setData(null);
    }

    @RequestMapping(value = "/nav/getNavByType",method = RequestMethod.POST)
    public Result<List<TbNav>> getNavByType(Integer type){
        List<TbNav> navList = contentService.getNavListByType(type);
        return new ResultUtil<List<TbNav>>().setData(navList);
    }

}
